package com.liujit.auth.security.jwt;

import com.liujit.common.constants.AuthConstant;
import lombok.RequiredArgsConstructor;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.stereotype.Component;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.util.Map;
import java.util.Set;

/**
 * @Description: JWT 生成工具类
 * @Author: liujun
 * @Date: 2021/6/28 11:52 上午
 */
@RequiredArgsConstructor
@Component
public class JwtGenerator {

    private final KeyPair keyPair;

    public String createAccessToken(Set<String> authorities, Map<String, Object> additional) {
        String payload = new JwtPayloadBuilder().exp(AuthConstant.EXP).authorities(authorities).additional(additional)
                .builder();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        RsaSigner signer = new RsaSigner(privateKey);
        String accessToken = JwtHelper.encode(payload, signer).getEncoded();
        return accessToken;
    }
}



